package io.alvin.ch04;

import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * Created by Alvin on 2017/7/12.
 */
public class AtomicIntegerArrayDemo {

    static AtomicIntegerArray arr = new AtomicIntegerArray(10);

    public static class AddThread implements Runnable {
        public void run() {
            for(int k = 0; k < 10000; k++) {
                arr.getAndIncrement(k%arr.length());
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final int numThreads = 12;
        Thread[] ts = new Thread[numThreads];
        for (int k = 0; k < numThreads; k++) {
            ts[k] = new Thread(new AddThread());
        }

        for (int k = 0; k < numThreads; k++) {
            ts[k].start();
        }

        for (int k = 0; k < numThreads; k++) {
            ts[k].join();
        }

        System.out.println(arr);
    }
}
